This sample is designed to illustrate how to use the OpenMP* API with the Intel® Fortran Compiler.

This program finds all primes in the first 10,000,000 integers, the number of 4n+1 primes, and the number of 4n-1 primes in the same range. It illustrates two OpenMP* directives to help speed up the code.

First, a dynamic schedule clause is used with the OpenMP* for directive. Because the for loop's workload increases as its index gets bigger, the default static scheduling does not work well. Instead, dynamic scheduling is used to account for the increasing workload. But dynamic scheduling itself has more overhead than static scheduling, so a chunk size of 10 is used to reduce the overhead for dynamic scheduling.

Second, a reduction clause is used instead of an OpenMP* critical directive to eliminate lock overhead. A critical directive would cause excessive lock overhead due to the one-thread-at-time update of the shared variables each time through the for loop. Instead the reduction clause causes only one update of the shared variables once at the end of the loop.

The sample can be compiled unoptimized (-O0 for Linux* or macOS*; /Od for Windows*), or at any level of optimization (-O1 through -O3 for Linux* or macOS*; /O1 through /O3 for Windows*). In addition, the following compiler options are needed.

Read the Intel® Fortran Compiler Documentation for more information about these options.

System Requirements:

Build Instructions

Use the one of the build.bat (Windows*) or makefile (macOS*; /Od for Windows*) targets:

Windows*: clean, debug, release, run (build [clean|debug|release|run)

Linux* and macOS*: clean, debug, release, run, debug_run (make [clean|debug|release|run|debug_run)

The sample is also provided as a Microsoft Visual Studio 2013* solution. It can also be used by Visual Studio 2015* and Visual Studio 2017*.